<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="服务项目" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入服务项目名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单用户" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入下单用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop/record:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
      v-loading="loading" 
      :data="recordList" 
      @selection-change="handleSelectionChange"
      :height="tableHeight"
      style="min-height: 300px; max-height: calc(100vh - 240px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录编号" align="center" prop="id" min-width="100" />
      <el-table-column label="服务项目" align="center" prop="name" min-width="150" />
      <el-table-column label="下单用户" align="center" prop="username" min-width="120" />
      <el-table-column label="实付金额" align="center" prop="price" min-width="120">
        <template slot-scope="scope">
          <span>￥{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销时间" align="center" prop="time" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺" align="center" prop="shop.name" min-width="100" />
      <el-table-column label="订单ID" align="center" prop="oid" min-width="100" />
      <el-table-column label="状态" align="center" prop="del" min-width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.del === '0' ? 'success' : 'danger'" size="mini">
            {{ scope.row.del === '0' ? '正常' : '已删除' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRecord } from "@/api/shop/record/record";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 核销记录表格数据
      recordList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        username: null,
      },
      // 表格高度
      tableHeight: null,
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.calculateTableHeight();
    // 监听窗口大小变化
    window.addEventListener('resize', this.calculateTableHeight);
  },
  beforeDestroy() {
    // 组件销毁前移除监听
    window.removeEventListener('resize', this.calculateTableHeight);
  },
  methods: {
    /** 查询核销记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/record/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    },
    /** 计算表格高度 */
    calculateTableHeight() {
      this.$nextTick(() => {
        // 获取视口高度
        const clientHeight = document.documentElement.clientHeight;
        // 减去其他元素的高度（头部、搜索栏、分页等）
        this.tableHeight = clientHeight - 240;
      });
    },
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
